
/**
 * Data Service
 * 数据接口、处理服务
 */

angular.module('ZJPC')
  .service('DataService', ['$http', '$q', '$interval', '$filter', 'CONST', DataService]);

function DataService($http, $q, $interval,$filter,CONST) {

  var that = this;
  var rest_url = CONST.rest_url;
  // var rest_url = 'http://172.28.27.179:8085/';

  //调用GET接口，返回promise
  this.restGetApi = function (url_path){
    var defered = $q.defer();
    var url = rest_url + url_path;
    $http.get(url).success(function (data) {
      defered.resolve(data);
    })
    .error(function (err) {
      defered.reject(err);
    });
    return defered.promise;
  };
    //调用Get接口，返回promise
  this.restGetApi = function (url_path, param){
    var defered = $q.defer();
    var url = rest_url + url_path;
    $http({
      method: 'GET',
      url: url,
      data: param
    }).success(function (data) {
      defered.resolve(data);
    })
    .error(function (err) {
      defered.reject(err);
    });
    return defered.promise;
  };
  //调用POST接口，返回promise
  this.restPostApi = function (url_path, param){
    var defered = $q.defer();
    var url = rest_url + url_path;
    $http({
      method: 'POST',
      url: url,
      data: param
    }).success(function (data) {
      defered.resolve(data);
    })
    .error(function (err) {
      defered.reject(err);
    });
    return defered.promise;
  };

    //调用delete接口，返回promise
  this.restDeleteApi = function (url_path, id){
    var defered = $q.defer();
    var url = rest_url + url_path + '/' +id;
    $http({
      method: 'DELETE',
      url: url,
      data:{}
    }).success(function (data) {
      defered.resolve(data);
    })
    .error(function (err) {
      defered.reject(err);
    });
    return defered.promise;
  };
/*  //设备位置
  this.getCamera = function(cb){
     this.restGetApi('/getAllTree').then(function(data){
          cb(data)
        },function(data){

      })    
  }*/

  //人员列表：教师与学生的混合表，jid为工号或学号
  this.getTimePersonList = function(starttime,endtime,page,pageNum,callback){
    starttime = $filter('date')(starttime, 'yyyy-MM-dd HH:mm:ss');
    endtime = $filter('date')(endtime, 'yyyy-MM-dd HH:mm:ss');
    this.restGetApi('/school/ykt/getPerTrack?starttime='+starttime+'&&endtime='+endtime+'&&page='+page+'&&pageNum='+pageNum).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

    })
  }

    //查询单人的轨迹信息（分页）
  this.getPersonTrackByPagin = function(starttime,endtime,page,pageNum,jid,callback){
    starttime = $filter('date')(starttime, 'yyyy-MM-dd HH:mm:ss');
    endtime = $filter('date')(endtime, 'yyyy-MM-dd HH:mm:ss');
    console.log('/school/ykt/getPerTrack?starttime='+starttime+'&&endtime='+endtime+'&&page='+page+'&&pageNum='+pageNum+'&&jid='+jid)
    this.restGetApi('/school/ykt/getPerTrack?starttime='+starttime+'&&endtime='+endtime+'&&page='+page+'&&pageNum='+pageNum+'&&jid='+jid).then(function(data){
        console.log(data)
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

    })
  }

    //人员列表：教师与学生的混合表，jid为工号或学号
  this.getTimePersonListByPagin = function(starttime,endtime,pageOption,callback){
    starttime = $filter('date')(starttime, 'yyyy-MM-dd HH:mm:ss');
    endtime = $filter('date')(endtime, 'yyyy-MM-dd HH:mm:ss');
    this.restGetApi('/school/ykt/getPersonListTime?starttime='+starttime+'&&endtime='+endtime+'&&pageNum='+pageOption.pageSize+'&&page='+pageOption.current).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

    })
  }

    //查询车辆列表
  this.getTimeCar = function(starttime,endtime,pageOption,callback){
    starttime = $filter('date')(starttime, 'yyyy-MM-dd HH:mm:ss');
    endtime = $filter('date')(endtime, 'yyyy-MM-dd HH:mm:ss');
    this.restGetApi('/school/ykt/getCarListTime?starttime='+starttime+'&&endtime='+endtime+'&&pageNum='+pageOption.pageSize+'&&page='+pageOption.current).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

    })
  }

      //查询车辆列表
  this.getTimeCarByPagin = function(starttime,endtime,pageoption,callback){
    starttime = $filter('date')(starttime, 'yyyy-MM-dd HH:mm:ss');
    endtime = $filter('date')(endtime, 'yyyy-MM-dd HH:mm:ss');
    this.restGetApi('/school/ykt/getCarListTime?starttime='+starttime+'&&endtime='+endtime+'&&pageNum='+pageoption.pageSize+'&&page='+pageoption.current).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

    })
  }

  //首页查询部分:人员
  //海康数据
  this.getPersonImg = function(gh,callback){
    var list = {
      imgurl:'../img/person/person1-origin.png',
      imgurlnew:'../img/person/person1-new.png',
      simacc:'85.01%',
      jid:gh
    }
    this.restGetApi('/school/ykt/getPersonImg?jid='+gh).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

      })
/*    return list;*/
  }

  //基本信息：根据工号搜索
  this.getPerson = function(name,callback){
 /*   //这里前端判断号码类型是工号还是学号，分为2个接口
    //学生接口
    var studentInfo = {
      id:'11111',
      rylx:1,
      xm:'赵伟杰',
      xh:gh,
      type:1,
      sjhm:'13888888888',
      xb:'男',
      csrq:'1995-10-10',
      jg:'浙江杭州',
      sfzjh:'311001222222211111',
      zz:'浙江杭州xxx区xxx路xxx号',
      xy:'警察学院',
      zy:'刑侦专业',
      bj:'刑侦二班',
      zp:'../img/person/person1-origin.png', //库存图
      mz:'汉族',
      zzmm:'共产党员',
      zxzt:'在校'
    }
    //职工接口
    var teacherInfo = {
      id:'11111',
      rylx:2,
      xm:'赵老师',
      jh:'12345',
      type:2,
      gh:gh,
      sjhm:'13888888888',
      xb:'男',
      csrq:'1995-10-10',
      jg:'浙江杭州',
      sfzjh:'311001222222211111',
      zz:'浙江杭州xxx区xxx路xxx号',
      dw:'警察学院',
      xzzw:'教导主任',
      zp:'../img/person/person1-origin.png', //库存图
      mz:'汉族',
      zzmm:'共产党员',
      zgxw:'学士',
      zgxl:'博士',
      zxzt:'在校',
      //根据工号查询得到的车辆信息，有则有，无则null
      carinfo:{
        id:'1111',
        cph:'浙A12345',
        syrxm:'赵伟杰',
        gh:gh, //工号匹配
        //以下为海康数据
        carstatus:'正常',
        engineNum:'12345678',//发动机号
        engineCapacity:'2.0V',//排量
        ratedpassenger:'4人',//核定载客，格式以数据为准
        carbrand:'大众',
        carcolor:'黑色',
        platetype:'92式民用车',
        cartype:'小型汽车',
        licensetype:'小型汽车',
        plateColor:'蓝色',
        license:'杭州车管所',//发证机关
        imgurl:'../img/car/car1.png' //车图
      }
    }*/
    this.restGetApi('/school/search/getPersonInfo?name='+name).then(function(data){
      console.log(data)
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){
      alert(data.msg)
      })
  } 

  //基本信息：根据车牌搜索
  this.getCar = function(cph,callback){
    var carInfo = {
      id:'1111',
      cph:cph, //车牌号：查询依据
      syrxm:'李强',
      gh:'1234056',
      ownerinfo:{
        id:'11111',
        jh:'12345',
        xm:'李强',
        gh:'1234056', //根据gh查询得到的车主信息，有则有，无则null
        sjhm:'13888888888',
        xb:'男',
        csrq:'1995-10-10',
        jg:'浙江杭州',
        sfzjh:'311001222222211111',
        zz:'浙江杭州xxx区xxx路xxx号',
        dw:'警察学院',
        zp:'../img/person/person1-origin.png', //库存图
        mz:'汉族',
        zzmm:'共产党员',
        zgxw:'学士',
        zgxl:'博士',
        zxzt:'在校',
        xzzw:'教导主任'     
      },
      //以下为根据车牌号查询出的海康数据
      carstatus:'正常',
      engineNum:'12345678',//发动机号
      engineCapacity:'2.0V',//排量
      ratedpassenger:'4人',//核定载客，格式以数据为准
      carbrand:'大众',
      carcolor:'黑色',
      platetype:'92式民用车',
      cartype:'小型汽车',
      licensetype:'小型汽车',
      plateColor:'蓝色',
      license:'杭州车管所',//发证机关
      zp:'../img/car/car1.png', //车图
    }
    this.restGetApi('/school/ykt/getCarInfo?cph='+cph).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

      })
  }

  this.getTeacherCar = function(jid,callback){
        this.restGetApi('/school/ykt/getCarInfo?jid='+jid).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

      })
  }

  //查询人轨迹信息(整合海康与其余的轨迹数据)
  this.getPerTrack = function(starttime,endtime,jid,callback){
    console.log(starttime, endtime);
    this.restGetApi('/school/way/getTrackAndWay?starttime='+starttime+'&&endtime='+endtime+'&&jid='+jid).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

      })
  }



  //查询车辆轨迹（卡口数据）
  this.getCarTrack = function(starttime,endtime,cph,callback){
    this.restGetApi('/school/way/getWay_car?starttime='+starttime+'&&endtime='+endtime+'&&cph='+cph).then(function(data){        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){
          callback(data)
      })
  }

  //查询车辆轨迹（卡口数据）
  this.getCarTrackByPagin = function(starttime,endtime,cph,callback,pageOption){
    var list = [
      { 
        cph:cph,
        barrName:'18号宿舍楼2',
        passTime:'2017-10-19 21:17:16',
        direction:'上行'
      },
      { 
        cph:cph,
        barrName:'南绪生活超市',
        passTime:'2017-10-19 21:17:16',
        direciton:'下行'
      }
    ]
    this.restGetApi('/school/ykt/getCarTrack?starttime='+starttime+'&&endtime='+endtime+'&&cph='+cph+'&&page='+pageOption.current+'&&pageNum='+pageOption.pageSize).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){
          callback(list)
      })
  }

/*  //获取同行车轨迹
  this.getCarTrackGroup = function(carGroup,starttime,endtime){
    //carGroup = ['浙A232323','浙B44489']
    //返回点位组
    var group = {
      '浙A23233':[{'33.999999','22.444444'},{'34.93333','22.44444'}],
      '浙B44489':[{'32.222222','25.444444'},{'33.66666','25.44444'}]
    }
    return group;
  }*/

/*  //获取同行人轨迹
  this.getCarTrackGroup = function(perGroup,starttime,endtime){
    //perGroup = ['2224444','544444','2222333'] <- 学号
    var group = {
      '2224444':[{'33.999999','22.444444'},{'34.93333','22.44444'}],
      '544444':[{'32.222222','25.444444'},{'33.66666','25.44444'}],
      '2222333':[{'35.222222','25.444444'},{'36.66666','25.44444'}]
    }
    return group;
  }*/


  //车辆的formertime:前跟车时间，latertime：后跟车时间,carPlate：车牌号（海康）
  this.getCarfellowcar=function(starttime,endtime,formertime,latertime,carPlate,callback){
    var car = [
      { 
        carPlate:'浙E89123',
        matchrate:'高',//匹配度
        plateColor:'蓝色',
        matchCount:'20',//匹配数量
        matchrate:'高'//匹配程度
      },
      {
        carPlate:'浙E89124',
        matchrate:'中', //匹配度
        plateColor:'黄色',
        matchCount:'20',//匹配数量
        matchrate:'高'//匹配程度
      }
    ]

    this.restGetApi('/school/ykt/getCarfellowcar?starttime='+starttime+'&&endtime='+endtime+'&&formertime='+formertime+'&&latertime='+latertime+'&&cph='+carPlate).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){
      console.log(data)
      })
  }

  //人员的同行车formertime:前跟车时间，latertime：后跟车时间,num:工号（海康）
  this.getPerfellowcar=function(starttime,endtime,formertime,latertime,cph,callback){
    var car = [
      { 
        carPlate:'浙E89123',
        matchrate:'高',//匹配度
        plateColor:'蓝色',
        matchCount:'20',//匹配数量
        matchrate:'高'//匹配程度
      },
      {
        carPlate:'浙E89124',
        matchrate:'中', //匹配度
        plateColor:'黄色',
        matchCount:'20',//匹配数量
        matchrate:'高'//匹配程度
      }
    ]
    this.restGetApi('/school/ykt/getPerfellowcar?starttime='+starttime+'&&endtime='+endtime+'&&formertime='+formertime+'&&latertime='+latertime+'&&cph='+cph).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

      })
  }

  //车辆的同行人formertime:前跟车时间，latertime：后跟车时间,carPlate：车牌号（海康）
  this.getCarfellowperson = function(starttime,endtime,formertime,latertime,gh,callback){
    var per = [
        {
          name:'张小杰',
          jid:'12345',
          matchrate:'高', //匹配度
          matchCount:'20'
        },
        {
          name:'赵杰',
          jid:'23456',
          matchrate:'中',
          matchCount:'10'
        }
    ]
    this.restGetApi('/school/ykt/getCarfellowperson?starttime='+starttime+'&&endtime='+endtime+'&&formertime='+formertime+'&&latertime='+latertime+'&&jid='+gh).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

      })
  }

  //人员的同行人formertime:前跟车时间，latertime：后跟车时间,num:工号(海康)
  this.getPerfellowperson = function(starttime,endtime,formertime,latertime,gh,callback){
    var per = [
      {
        name:'张小杰',
        jid:'12345',
        matchrate:'高' //匹配度
      },
      {
        name:'赵杰',
        jid:'23456',
        matchrate:'中'
      }
    ]
    this.restGetApi('/school/ykt/getPerfellowperson?starttime='+starttime+'&&endtime='+endtime+'&&formertime='+formertime+'&&latertime='+latertime+'&&jid='+gh).then(function(data){
      if(data.status==200){
        callback(data.data)
      }else{
        alert(data.msg)
      }
    },function(data){


      })
  }

  this.getWifiByLsh = function(lsh,callback){
    this.restGetApi('/school/track/getWifiByLsh?lsh='+lsh).then(function(data){
      if(data.status==200){
        callback(data.data)
      }else{
        alert(data.msg)
      }
    },function(data){

    })
  }

  this.getCardDetail = function(lsh,ly,callback){
    this.restPostApi('/school/track/getCardDetail?lsh='+lsh+'&&ly='+ly).then(function(data){
      if(data.status==200){
        callback(data.data)
      }else{
        alert(data.msg)
      }
    },function(data){
        alert(data.msg)
    })
  }



  //消费
  this.getxfjl = function(starttime,endtime,jid,callback){
    var xf = [
      {
        xm:'黄志斌',
        jid:jid,
        jysj:'2017-10-23 13:37:15',
        sh:'图书馆',
        shmc:'图书馆',//商户名称
        jye:1, //交易额
        lsh:'',
        ye:'',
        shlxmc:''
      }
    ]
    this.restGetApi('/school/ykt/gexfjl?starttime='+starttime+'&&endtime='+endtime+'&&jid='+jid).then(function(data){
        if(data.status==200){
          callback(data.data)
        }else{
          alert(data.msg)
        }
    },function(data){

      })
  }



  //获取一卡通数据
  this.getIcard = function(starttime, endtime, list, callback){
    var cardlist = [
      {
        id : '1', 
        name : '王小明123',
        time : '2017-10-23 13:27:17',
        place: '18号宿舍楼',
        action:'门禁'
      },
      { 
        id : '2',
        name : '王大陆123', 
        time : '2017-10-23 13:27:17', 
        place: '2号宿舍楼', 
        action:'门禁'
      }
    ];
    this.restGetApi('/school/track/getIcard?starttime='+starttime+'&&endtime='+endtime+'&&jid='+'&&list=' + list).then(function(data){
        console.log(1,data)
        if(data.status==200){
          callback(data.data);
        }else{
          alert(data.msg)
        }
    },function(data){

      })

  }

  //获取wifi数据
  this.getWifi = function (starttime,endtime){
    var wifilist = [
      {
        id : '1', 
        name : '王小明123',
        time : '2017-10-23 13:27:17',
        place: '18号宿舍楼',
        duration: '20分钟'
      },
      { 
        id : '2',
        name : '王大陆123', 
        time : '2017-10-23 13:27:17', 
        place: '2号宿舍楼', 
        duration: '20分钟'
      }
    ]
    return wifilist;
  }
  //获取车辆卡口数据  carNum为车牌号码
  this.getBayonet = function (carNum,starttime,endtime){
    var bayonetlist = [
      { 
        id : '1', 
        carNum : '浙ASJ867', 
        bayonetName : '天竺路东', 
        elapsedTime: '2017-10-23 13:27:17', 
        license:'92式民用车',
        vehicleType:'大型汽车',
        vehicleBrand:'福特',
        homeLocation:'浙江省杭州市'
      },
      { 
        id : '2', 
        carNum : '浙ABJ867', 
        bayonetName : '校门口球机1', 
        elapsedTime: '2017-10-23 14:27:17', 
        license:'92式民用车',
        vehicleType:'大型汽车',
        vehicleBrand:'长城',
        homeLocation:'浙江省杭州市'
      }
    ]
    return bayonetlist;
  }

  //获取人脸识别数据
  this.getFace = function (starttime,endtime){
    var facelist = [
      { 
        id : '1', 
        picUrl : '/img/person/person1-new.png', 
        time : '2017-10-23 13:48:12'
      },
      { 
        id : '2', 
        picUrl : '/img/person/person1-new.png', 
        time : '2017-10-23 13:48:12'
      }
    ]
    return facelist;
  }

  //获取树形图的数据
  this.getTreeData = function (callback){

    this.restGetApi('school/getAllTree').then(function (respon){
      // this.restGetApi('getAllTree').then(function (respon){
      // console.log(respon,'---');
      callback(respon);
    })

  }

  //深拷贝
  this.deepCopy = function (p, c){

      var c = c || {};  
      for (var i in p) {  
          if(! p.hasOwnProperty(i)){  
              continue;  
          }  
          if (typeof p[i] === 'object' && p[i]) {  
              c[i] = (p[i].constructor === Array) ? [] : {};  
              this.deepCopy(p[i], c[i]);  
          } else {  
              c[i] = p[i];  
          }  
      }  
      return c;  

  }

  this.mergeTrack = function (data) {
    var result = {};
    for(var i = 0; i < data.length; i++) {
      if(data[i].position==undefined){
        data[i]['position'] = data[i].kkmc;
        data[i]['passTime'] = data[i].jgsj;
      }
      if(!result[data[i].position]) {
        result[data[i].position] = {
          addr: data[i].position,
          picture: '',
          times: [],
          camerJd: data[i].camerJd,
          camerWd: data[i].camerWd
        };
        if(data[i].zpzp) {
          result[data[i].position].picture = data[i].zpzp;
        }
        if(data[i].zp) {
          result[data[i].position].picture = data[i].zp;
        }
      }
      result[data[i].position].times.push(data[i].passTime);
    }
    console.log(result);
    return result;
  }



}